package com.youlai.boot.system.mapper;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.youlai.boot.common.annotation.DataPermission;
import com.youlai.boot.system.model.dto.LedgerCountVO;
import com.youlai.boot.system.model.entity.SysLedger;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.youlai.boot.system.model.query.SysLedgerQuery;
import com.youlai.boot.system.model.vo.SysLedgerExcelVO;
import com.youlai.boot.system.model.vo.SysLedgerVO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.List;

/**
 * 流水台账Mapper接口
 *
 * @author youlaitech
 * @since 2025-05-12 15:55
 */
@Mapper
public interface SysLedgerMapper extends BaseMapper<SysLedger> {

    /**
     * 获取流水台账分页数据
     *
     * @param page 分页对象
     * @param queryParams 查询参数
     * @return
     */
    @DataPermission
    Page<SysLedgerVO> getSysLedgerPage(Page<SysLedgerVO> page, SysLedgerQuery queryParams);

    /**
     * 查询列表
     * @param queryParams
     * @return
     */
    @DataPermission
    List<SysLedgerExcelVO> exportList(@Param("queryParams")SysLedgerQuery queryParams);

    /**
     * 统计流水台账数据
     * @param queryParams
     * @return
     */
    @DataPermission
    LedgerCountVO getSysLedgerCount(@Param("queryParams")SysLedgerQuery queryParams);

    /**
     *
     * @param id
     * @param chepaihao
     * @param day
     * @return
     */
   default SysLedger selectByChepaihao(Long id, String chepaihao, Long day){
       LocalDate endDate = LocalDate.now();
       LocalDate startDate = endDate.minusDays(day);
       LambdaQueryWrapper<SysLedger> lqw = new LambdaQueryWrapper<>();
       if (id != null){
           lqw.ne(SysLedger::getId, id);
       }
       lqw.eq(SysLedger::getChepaihao, chepaihao);
       lqw.ge(SysLedger::getCreateTime, LocalDateTime.of(startDate, LocalTime.MIN));
       lqw.le(SysLedger::getCreateTime, LocalDateTime.of(endDate, LocalTime.MAX));
       lqw.orderByDesc(SysLedger::getId);
       lqw.last("limit 1");
       return this.selectOne(lqw);
   }
}
